<?php
/**
 * @file rpd.php
 *
 * This is the frontend controller of rpd.
 *
 * Every client-input will be filtered in this file. It incrementaly searches
 * the right php and stores additional input in $_GET.
 *
 *
 * See also:
 *  @see configuration.php
 *  @see bootstrap.php
 *
 * BSD License
 * \n
 * Copyright (c) 2010 Rpd-Team (http://code.google.com/p/rpd)
 *
 * @namespace  rpd
 * @since      0.0.1
 * @version    0.3.0
 *
 */

/**
 *
 * Loads unknown classes dynamicaly.
 * @param string $class the unknown class
 */
function __autoload($class)
{
	$class = str_replace("_", "/", $class);
	if (file_exists("rpd/" . $class . ".php"))
	{
		require_once "rpd/" . $class . ".php";
	}
	else
	{
		require_once "app/" . $class . ".php";
	}
}

/*
 *
 * Make useable PHP environment
 */
include("rpd/php/additional.php");			// generate additional functions

// workaround old versions
if (version_compare(PHP_VERSION, "5.3.0", "<"))
{
	include("rpd/php/5-3-0.php");			// below 5.3.0
}
else
{

}


/**
 *
 * @bug server[redirect_url] contain "//" on calling root "/"
 */

ob_start();

require_once "configuration.php";		// some configuration
require_once "bootstrap.php";			// user defined php before rpd acts

/*
 * }}}
 *
 */
if (isset($_SERVER["REDIRECT_URL"]))
{
	$_SERVER["REDIRECT_URL"] = strtolower(
                                          str_replace(ROOT,
                                                      "",
                                                      $_SERVER["REDIRECT_URL"]
                                                     ) . "/"
                                          );
}
else
{
	$_SERVER["REDIRECT_URL"] = NULL;
}

$dir = NULL;
$tmp = strtok($_SERVER["REDIRECT_URL"], "/");	///< temporary variable
$file = "index";

/**
 *
 * Enter description here ...
 * @var string
 */
$ext = "." . pathinfo($_SERVER["REDIRECT_URL"], PATHINFO_EXTENSION);
if ($ext == ".ajax")
{
	header ('Content-Type: text/xml', true);
	define("EXTENSION", $ext);
}
else
{
	define("EXTENSION", NULL);
}

while (
	$tmp != false
	&& (
		is_dir("app/View/" . $dir . $tmp . "/")
		|| is_dir("app/Controller/" . $dir . $tmp . "/")
	)
)
{
	// lookup bootstrap
	if (file_exists("app/Controller/" . $dir . $tmp . "/_bootstrap.php"))
	{
		$bootstrap[] = "app/Controller/" . $dir . $tmp . "/_bootstrap.php";
	}

	// go into dir
	$dir .= $tmp . "/";
	$file = $tmp;
	$tmp = strtok("/");
}

if ($ext !== ".")
{
	$file .= EXTENSION;
	$tmp = basename($tmp, $ext);
	if (is_dir("app/View/" . $dir . $tmp . "/")
	|| is_dir("app/Controller/" . $dir . $tmp . "/"))
	{
		$file = $tmp . EXTENSION;
		$dir .= $tmp . "/";
		$tmp = strtok("/");
	}
}
else
{
	$ext = "";
}

/**
 *
 * Executed Directory.
 * \n
 * Depending on the called URL (by Client) the system searches the nearest
 * (lower) path wich contains a executable PHP-File. The name of this folder
 * is stored here.
 */
define("DIR", strtolower($dir));

/**
 *
 * Executed file.
 * \n
 * The client calls a URL to which the system searches the nearest (lower)
 * executable PHP-File. The name of this file is contained here.
 */
define("FILE", strtolower($file));

while ($tmp !== false)
{
	$_GET[$tmp] = strtok("/");
	$tmp = strtok("/");
}

// run bootstraps for each path
foreach ($bootstrap as $file)
{
	include $file;
}

// create site if none is given
if (!isset($site))
{
	$site = new Controller();
}

echo $site;		// dispatch

?>